<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Admin_Disclaimer_Ctrl extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by  = 'created_date', $sort_order = 'asc', $current_page = 0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
				//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}
			
			$results_per_page = 4;
			
			$url = 'http://localhost/azindia/azindia-api/en/json.api';
			$data = array('www-command'=>'articles-get_disclaimers','current_page'=>$current_page,'results_per_page'=>$results_per_page,'order_by'=>$order_by,'sort_order'=>$sort_order,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			if($response->message !== 'No Disclaimers'){
			$this->load->library('pagination');
			$config['base_url'] = 'http://localhost/azindia/azindia-client/admin/admin_disclaimer_ctrl/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();			
			$data['articles_get_disclaimers'] = $response;
			$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['articles_get_disclaimers'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['disclaimer_fileds'] = array('disclaimer_name'=>'Disclaimer Name', 'description'=>'Description', 'status'=>'Status');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['search_by_value'] = $search_by_value;
			$data['search_by'] = $search_by;
			$data['status'] = array(1=>'Active',0=>'In-Active');
			
			$this->load->view('admin/disclaimer_view',$data);
		}
		
		function search_by(){
		
		if($this->input->post('search') == ''){
			$search = $this->input->post('search1');
		}else{
			$search = $this->input->post('search');
		}
			
		$query_array = array(
			'search_by'=>$this->input->post('search_by'),
			'search'=>$search
		);
		$query_id=rand();
		$_SESSION['search']=$query_array;
		//echo '<pre>';print_r($_SESSION['search']);exit;
		//$this->session->set_userdata('disclaimer_search',$query_array);
		redirect('admin/admin_disclaimer_ctrl/index/'.$query_id);
	}
	
		function create_disclaimer_page(){
			$this->load->view('admin/create_disclaimer');
		}
		
		function create_disclaimer(){
				$this->form_validation->set_error_delimiters('<span style="color:red">', '</span>');
				$this->form_validation->set_rules('disclaimer_name', 'Disclaimer Name', 'required|alpha|min_length[3]|max_length[15]');
				
				if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('admin/create_disclaimer');
				
			}else{
				
				$url = 'http://localhost/azindia/azindia-api/en/json.api';
				$data = array('www-command'=>'articles-create_disclaimer','disclaimer_name'=>$this->input->post('disclaimer_name'),'description'=>$this->input->post('description'));
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				redirect('admin/admin_disclaimer_ctrl');
			}	
		}
		
		function edit_disclaimer_page(){
				$url = 'http://localhost/azindia/azindia-api/en/json.api';
				if($this->uri->segment(4) > 0){
					$_SESSION['disclaimer_id'] = $this->uri->segment(4);
				}
				$data = array('www-command'=>'articles-get_disclaimer','disclaimer_id'=>$_SESSION['disclaimer_id']);
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				$response = (array) $response;
				foreach($response as $value){
					$value = $value;
				}
				//echo '<pre>';print_r($value);exit;
				$data['disclaimer_id'] = $value->disclaimer_id;
				$data['disclaimer_name'] = $value->disclaimer_name;
				$data['description'] = $value->description;
				$this->load->view('admin/edit_disclaimer',$data);
				
		}
		
		function edit_disclaimer(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">', '</span>');
				$this->form_validation->set_rules('disclaimer_name', 'Disclaimer Name', 'required|alpha|min_length[3]|max_length[15]');
				
				if ($this->form_validation->run() == FALSE)
			{
				//echo form_error('disclaimer_name');exit;
				$data['disclaimer_id'] = $this->input->post('disclaimer_id');
				$data['disclaimer_name'] = $this->input->post('disclaimer_name');
				$data['description'] = $this->input->post('description');
				$this->load->view('admin/edit_disclaimer',$data);
				
			}else{
				$url = 'http://localhost/azindia/azindia-api/en/json.api';				
				$data = array('www-command'=>'articles-update_disclaimer','disclaimer_id'=>$this->input->post('hidden_id'),'disclaimer_name'=>$this->input->post('disclaimer_name'),'description'=>$this->input->post('description'));
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				redirect('admin/admin_disclaimer_ctrl');
			}
		}
		
		function delete_disclaimer(){
			//echo $this->uri->segment(4);exit;
			$url = 'http://localhost/azindia/azindia-api/en/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'articles-delete_disclaimer','status'=>$status,'disclaimer_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				redirect('admin/admin_disclaimer_ctrl');
		}
		
		

		
	}
	
?>